package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;
/**
 * @Author DELL
 * @Date 2021/1/26 19:56
 * @Version 1.0
 */


public class Planet extends Star{

    public Star star;

    public Planet(){

    }
    public Planet(Star star,String imageKey,double AU,double e,int T,String name){
        this.star = star;
        this.img = ImageUtil.maps.get(imageKey);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.longAxis = getLongAxisByAU(AU);
        this.shortAxis = getShortAxisByE(e);
        this.x = Constant.WINDOW_WIDTH / 2 + longAxis;
        this.y = Constant.WINDOW_HEIGHT / 2;
        this.speed = getSpeedByT(T);
        this.theta = 0.0;
        this.name = name;
    }

    public Planet(Sun sun,String imageName,double AU,double e,int T,String name,boolean smallPlanet){
        this(sun,imageName,AU,e,T,name);
        this.smallPlanet = smallPlanet;
    }
    public Planet(Sun sun,String imageName,double AU,double e,int T,String name,boolean smallPlanet,boolean venus){
        this(sun,imageName,AU,e,T,name,smallPlanet);
        this.venus = venus;
    }

    private double getSpeedByT(int t) {
        return 0.1 * 365 / t;
    }

    private int getLongAxisByAU(double AU){
        return (int) (100 * AU) / 2 ;
    }

    private int getShortAxisByE(double e){
        return (int)(this.longAxis * Math.sqrt(1 - e * e));
    }

    @Override
    public void draw(Graphics g) {

        Color c = g.getColor();
        g.setColor(Color.WHITE);
        if(!smallPlanet){
            drawTrace(g);
        }
        Font f = g.getFont();
        g.setFont(new Font("幼圆",Font.BOLD,24));
        g.drawString(name,x, y - 30);
        g.setFont(f);
        g.setColor(c);


        g.drawImage(img,x,y,null);

        move();
    }

    private void drawTrace(Graphics g){
        int x = (int) (this.star.x - this.longAxis + star.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
        int y = this.star.y - this.shortAxis + star.height / 2;
        int width = 2 * this.longAxis;
        int height = 2 * this.shortAxis;
        g.drawOval(x,y,width,height);
    }

    @Override
    public void move() {
        if(!venus){
            x = (int) ((longAxis * Math.sin(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int) (shortAxis * Math.cos(theta)) + star.y + star.height / 2 - this.height / 2;
        }else{
            x = (int) ((longAxis * Math.cos(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int) (shortAxis * Math.sin(theta)) + star.y + star.height / 2 - this.height / 2;
        }
        theta += speed;
    }
}
